home *** CD-ROM | disk | FTP | other *** search
- Path: dawn.mmm.com!news
- From: kjhopps@mmm.com (Kevin J Hopps)
- Newsgroups: comp.lang.c++
- Subject: Re: meaning of int a::*b
- Date: 20 Feb 1996 16:57:47 GMT
- Organization: 3M - St. Paul, MN 55144-1000 US
- Message-ID: <4gcuib$507@dawn.mmm.com>
- References: <31284007.7977@mercury.co.il>
- Reply-To: kjhopps@mmm.com
- X-Newsreader: TIN [version 1.2 PL2]
-
- Shlomo Wygodny (wygodny@mercury.co.il) wrote:
- > In the following (compilable) program:
-
- > class a{};
- > int a::*b;
- > void main(){}
-
- > Does someone know what is the meaning of int a::*b; ?
- > It's probably a declaration of a variable b of type int a::* .
- > But what
- > does it mean? What can be assigned to it?
-
- The variable b is a pointer to an int member of class a. It
- might help to think of b as an offset rather than a pointer.
- Given an object of class a, and the offset b, you can find the
- int member within the a that b refers to.
-
- For example:
- #include <iostream.h>
- class a
- {
- public:
- int i1;
- int i2;
- };
- int a::*b;
- int main()
- {
- a x;
- x.i1 = 1;
- x.i2 = 2;
- b = &a::i1; // "offset" of i1 in an "a."
- cout << x.*b << '\n'; // prints 1
- b = &a::i2; // "offset" of i2 in an "a."
- cout << x.*b << '\n'; // prints 2
- return 0;
- }
- --
- Kevin J. Hopps e-mail: kjhopps@mmm.com
- 3M Company phone: (612) 737-4643
- 3M Center, Bldg. 235-2D-57 fax: (612) 737-2700
- St. Paul, MN 55144-1000 Opinions are my own. I don't speak for 3M.
- But 3M speaks for me -- I did not write the following line:
-
- Opinions expressed herein are my own and may not represent those of 3M.
-